package com.sjb.mianshiya.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjb.mianshiya.mapper.common.ErrorCode;
import com.sjb.mianshiya.exception.BusinessException;
import com.sjb.mianshiya.model.domain.Question;
import com.sjb.mianshiya.model.domain.QuestionBankQuestion;
import com.sjb.mianshiya.model.request.question.QuestionQueryRequest;
import com.sjb.mianshiya.model.vo.QuestionVO;
import com.sjb.mianshiya.service.QuestionBankQuestionService;
import com.sjb.mianshiya.service.QuestionService;
import com.sjb.mianshiya.mapper.QuestionMapper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author dell
* @description 针对表【question(题目)】的数据库操作Service实现
* @createDate 2024-09-17 15:40:16
*/
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question>
    implements QuestionService{

    @Resource
    private QuestionBankQuestionService questionBankQuestionService;

    @Resource
    @Lazy
    private QuestionService questionService;

    @Override
    public void validQuestion(Question question, boolean b) {

    }

    @Override
    public QuestionVO getQuestionVO(Question question, HttpServletRequest request) {
        return null;
    }

    @Override
    public Wrapper<Question> getQueryWrapper(QuestionQueryRequest questionQueryRequest) {
        return null;
    }

    @Override
    public Page<QuestionVO> getQuestionVOPage(Page<Question> questionPage, HttpServletRequest request) {
        return null;
    }

    /**
     * 分页获取题目列表信息
     * @param questionQueryRequest
     * @return
     */
    @Override
    public Page<Question> listQuestionByPage(QuestionQueryRequest questionQueryRequest) {
        // 题库ID
        Long questionBankId = questionQueryRequest.getQuestionBankId();
        int pageSize = questionQueryRequest.getPageSize();
        int pageNum = questionQueryRequest.getPageNum();
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        if (questionBankId != null){
            // 题库题目关联表 题库关联的所有题目ID
            LambdaQueryWrapper<QuestionBankQuestion> lambdaQueryWrapper = Wrappers.lambdaQuery(QuestionBankQuestion.class)
                    .select(QuestionBankQuestion::getQuestionid)
                    .eq(QuestionBankQuestion::getId, questionBankId);

            List<QuestionBankQuestion> questionBankQuestionList = questionBankQuestionService.list(lambdaQueryWrapper);
            Set<Long> questionId = questionBankQuestionList.stream().map(QuestionBankQuestion::getQuestionid)
                    .collect(Collectors.toSet());
            queryWrapper.in("id",questionId);
        }else {
            return new Page<>(pageNum, pageSize,0);
        }
        return questionService.page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatchQuestion(List<Long> questionIds) {
        if (CollUtil.isEmpty(questionIds)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "要删除的题目列表为空");
        }
        for (Long questionId : questionIds) {
            boolean remove = this.removeById(questionId);
            if (!remove){
                throw new BusinessException(ErrorCode.OPERATION_ERROR,"移除题目失败");
            }

            // 移除题目题库关联关系
            LambdaQueryWrapper<QuestionBankQuestion> questionLambdaQueryWrapper = Wrappers.lambdaQuery(QuestionBankQuestion.class)
                    .eq(QuestionBankQuestion::getQuestionid, questionId);
            remove = questionBankQuestionService.remove(questionLambdaQueryWrapper);
            if (!remove){
                throw new BusinessException(ErrorCode.OPERATION_ERROR,"移除题库题目关联异常");
            }
        }
    }
}




